package com.giw.uima;


import org.apache.uima.TokenAnnotation;
import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.Type;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import com.giw.uima.PersonNameAnnotation;
import java.lang.Character;

public class PersonNameAnnotator extends JCasAnnotator_ImplBase {
	private static final String POS_FEATURENAME = "posTag";

	
	@Override
	public void process(JCas jcas) throws AnalysisEngineProcessException {
		// TODO Auto-generated method stub
		/*
		 * int start = 0; for (Annotation annotation : jcas
		 * .getAnnotationIndex(TokenAnnotation.type)) { /* * create a name
		 * annotation for each occurrence of a name in the dictionary
		 */
	//	new NameAnnotator().process(jcas);
		for (Annotation annName : jcas.getAnnotationIndex(NameAnnotation.type)) {
			
			int begin = annName.getBegin();
			int end = annName.getEnd();
			boolean flag=false;
			
			for (Annotation annToken : jcas.getAnnotationIndex(TokenAnnotation.type)) {

				if (annToken.getBegin() >= annName.getEnd()&& !annToken.getCoveredText().isEmpty()
					 ) {
					/*
					 * Caso in cui abbiamo  Marco Rossi is boy.
					 */
					if (Character.isUpperCase(annToken.getCoveredText().charAt(0)) && (isValore(annToken, "nn")|| (isValore(annToken,"nns")))) {
						end = annToken.getEnd();
						PersonNameAnnotation personNameAnnotation = new PersonNameAnnotation(jcas);
						personNameAnnotation.setBegin(begin);
						personNameAnnotation.setEnd(end);
						personNameAnnotation.addToIndexes();
						break;
						
						}
					/*
					 * Caso in cui Marco is boy
					 */
					else if (!Character.isUpperCase(annToken.getCoveredText().charAt(0))){
						PersonNameAnnotation personNameAnnotation = new PersonNameAnnotation(jcas);
						personNameAnnotation.setBegin(begin);
						personNameAnnotation.setEnd(end);
						personNameAnnotation.addToIndexes();
						break;
					}
					/*
					 * Caso in cui abbiamo Marco Antonio Rossi is boy.
					 */
					else if (!flag && isValore(annToken,"np") && Character.isUpperCase(annToken.getCoveredText().charAt(0))){
						end = annToken.getEnd() ;
					}
					/*
					 * Caso in cui abbiamo Marco J. Rossi is boy
					 * Marco ha la palla di Antonio. Luigi è un bravo ragazzo	
					 */
					else if (isValore(annToken, ".")&& !flag) {
							flag=true;
							end = annToken.getEnd() - 1; }
					else if (flag){
						
							PersonNameAnnotation personNameAnnotation = new PersonNameAnnotation(jcas);
							personNameAnnotation.setBegin(begin);
							personNameAnnotation.setEnd(end);
							personNameAnnotation.addToIndexes();
							break;
							}
					}
				
				}
			}
		
		/*
Camillo Barack live in Milano.
Marco J. Valeri is born in Anna. 
Marco Valeri is born in Trope. 
Lorena Anotia live in Rome..
		 */

	}

	private boolean isValore(Annotation annotation, String valore) {
		Type type = annotation.getType();
		String pos = annotation.getFeatureValueAsString(type
				.getFeatureByBaseName(POS_FEATURENAME));

		return pos != null && pos.equals(valore);
	}

}